#coding:utf8
from config import *
import multiprocessing as mp  
from routin import *
import queue

mouse_input_q = queue.Queue()

def moveF():    
    while True:
        time.sleep(0.01)                    
        if time.time() - cfg.set_fix_time < 0.02:
            if get_cur_pos() != cfg.fix_pos:
                print('diff', time.time() - cfg.set_fix_time, get_cur_pos(), cfg.fix_pos)
                cfg.fix_pos = get_cur_pos()
            
        if time.time() - cfg.click_time > 0.2 and cfg.click_time >= 0:
            cfg.click_time = -1
            mouse.Controller().click(mouse.Button.left)            
            
        if cfg.move_task != None:
            dx, dy , ty= cfg.move_task
            d = move_task_delay1
            if ty == 2:
                d = move_task_delay2            
            if time.time() - cfg.last_move_time > d:            
                cfg.move_task = None
                x, y = get_cur_pos()
                cfg.fix_pos = (x + dx, y + dy)
                cfg.set_fix_time = time.time()
                try:
                    mouse.Controller().move(dx, dy)
                    deal_reset_one()
                except Exception as e:
                    print(e)

qq = None
def handle_event(e): #: winrawin.RawInputEvent
    if str(e.name)=='thumb1' and str(e.code)=='4':
        if str(e.event_type)=='up':
            qq.put(( 't1up',0,0,0,0))
        else:
            qq.put(( 't1down',0,0,0,0))
    if str(e.name)=='thumb2' and str(e.code)=='5':
        if str(e.event_type)=='up':
            qq.put(( 't2up',0,0,0,0))
        else:
            qq.put(( 't2down',0,0,0,0))

    if e.event_type == 'move':
        qq.put(( 'm', e.device.handle, e.delta_x, e.delta_y, time.time()))
    if e.event_type == 'wheel-up':
        qq.put(( 'w', e.device.handle, int(e.code), e.delta_y, time.time()))
    if e.event_type == 'wheel-down':
        qq.put(( 'w', e.device.handle, -int(e.code), e.delta_y, time.time()))     
      
def pro_input(q):
    global qq
    qq = q
    import winrawin
    import tkinter as tk    
    window = tk.Tk()
    window.maxsize(50, 50)
    winrawin.hook_raw_input_for_window(window.winfo_id(), handle_event)
    window.mainloop()

def jump_bg_thr():
    co = 0
    while True:        
        if check_jump():
            co += 1
            dx, dy = cal_random(co)            
            mouse.Controller().move(dx, dy)

def add_mouse(h):
    if len(cfg.left_mouse) == 0:
        cfg.left_mouse.append(h)
        print('add mouse', cfg.left_mouse, cfg.right_mouse)
    if h in cfg.left_mouse:
        return
    if h in cfg.right_mouse:
        return
    cfg.right_mouse.append(h)
    print('add mouse', cfg.left_mouse, cfg.right_mouse)

def for_mouse_jump(h):    
    add_mouse(h)
    if h in cfg.left_mouse:
        cfg.left_mouse_time = time.time()
    if h in cfg.right_mouse:
        cfg.right_mouse_time = time.time()
 
def deal_x2_move(x, y):
    dx = cfg.snap[1][1] - cfg.snap[0][1] 
    dy = cfg.snap[1][2] - cfg.snap[0][2]
    du = math.atan2(dy, dx)
    du2 = math.atan2(y, x)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    dd = math.cos(d) *math.sqrt(x * x + y * y) *get_x2_d_sca()
    cfg.x2_mode_d += dd
    cfg.x2_mode_d = min(100, cfg.x2_mode_d)
    cfg.x2_mode_d = max(0, cfg.x2_mode_d)        
    x2_d_to_dst()
        
def pro_get(q):
    while True:
        aa = q.get()
        ty, a, x, y, t = aa
        if ty=='t1up':
            on_click2(0,0,'x1',False,True)
            continue
        if ty=='t1down':
            on_click2(0,0,'x1',True,True)
            continue
        if ty=='t2up':
            on_click2(0,0,'x2',False,True)
            continue
        if ty=='t2down':
            on_click2(0,0,'x2',True,True)
            continue

        if ty == 'w':
            if str(a) == 'None':
                continue
            if not cfg.w2showing:
                continue
            if cfg.force_scroll:
                continue
            if cfg.enter_pressed:
                cfg.enter_pressed_then_other = True
                continue
            if cfg.x2_pressed:
                cfg.x2_pressed_then_other = True
                continue
            if cfg.x2_mode:                
                if x > 0:                    
                    for i in range(len(cfg.snap)):
                        d =i * 100 / (len(cfg.snap) - 1)  
                        if  d> cfg.x2_mode_d:
                            cfg.x2_mode_d =  d
                            break
                else:
                    for i in range(len(cfg.snap)):
                        d = (len(cfg.snap) - 1- i) * 100 / (len(cfg.snap) - 1)
                        if  d < cfg.x2_mode_d:
                            cfg.x2_mode_d = d
                            break
                find_near_d()
                x2_d_to_dst()
                continue
            if cfg.scroll_one==0:
                if a in cfg.left_mouse:
                    cfg.scroll_one = 1
                else:
                    cfg.scroll_one = 2
            if cfg.scroll_one==1:
                dd = 1
                if x<0:
                    dd = -1
                deal_manu_scroll(dd)
            else:
                to_lock.acquire()            
                if x > 0:
                    cfg.to = cut_to(cfg.to+get_scroll_sca())                    
                else:
                    cfg.to = cut_to(cfg.to-get_scroll_sca())                    
                set_to_chg()
                to_lock.release()                
            continue
        
        aa = (a, x, y, t)
        if cfg.x2_mode:
            if cfg.x2_pressed:                
                cfg.x2_pressed_then_other = True
                deal_x2_move(x, y)                
                find_near_d(True)
        deal_mouse_mode(a, x, y)
        mouse_input_q.put(aa)
        for_mouse_jump(a)                       
     
def check_to(a):    
    isx = True
    x1, y1, x2, y2 = a
    xx = x1 + x2
    yy = y1 + y2
    d3 = math.sqrt(xx * xx + yy * yy)
    d1 = math.sqrt(x1 * x1 + y1 * y1)
    d2 = math.sqrt(x2 * x2 + y2 * y2)
    du = math.atan2(y1, x1)
    du2 = math.atan2(y2, x2)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi   
    
    if d3 > max(d2 , d1):
        return 0,isx
    
    if d1 < 1 or d2 < 1:
        return 0,isx
        
    if abs(d) < math.pi * 0.5:
        return    0,isx
    a = -1
    
    if abs(x2) > abs(y2):
        if x2 > 0:
            a = 1
    else:
        isx = False
        if y2 > 0:
            a = 1    
    return a * max(d1, d2),isx

def one_loop():
    dx1 = 0
    dy1 = 0
    dx2 =0
    dy2 = 0    
    while not mouse_input_q.empty():               
        a = mouse_input_q.get()        
        h, x, y, t = a
        if h in cfg.left_mouse:
            dx1 += x
            dy1 += y
        else:
            dx2 += x
            dy2 += y
    return dx1, dy1, dx2, dy2

def read_mouse_q_thr():        
    while True:
        time.sleep(0.05)
        a = one_loop()       
        dd,isx = check_to(a)       
        if dd == 0:
            continue
        if not cfg.w2showing:
            continue        
        if (cfg.to <= 0 and dd < 0) or (cfg.to >= max_l and dd > 0):
            cfg.to_time = time.time()
            continue
        to_lock.acquire()        
        cfg.to = cut_to(cfg.to+dd*get_to_sca())
        cfg.to_time = time.time()
        set_to_chg()
        cfg.scroll_one = 0
        to_lock.release()                
    